package com.cai.quartz.schedule;

import com.cai.quartz.job.MyListenerJob;
import com.cai.quartz.listener.job.MyJobListener1;
import com.cai.quartz.listener.scheduler.MySchedulerListener;
import com.cai.quartz.listener.trigger.MyTriggerListener;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.matchers.KeyMatcher;
import org.springframework.context.annotation.Configuration;

import java.util.Date;

@Configuration
public class MyListenerScheduler3 {
    public MyListenerScheduler3() throws SchedulerException, InterruptedException {
        doSomeThing();
    }

    public void doSomeThing() throws SchedulerException, InterruptedException {
        Scheduler scheduler= StdSchedulerFactory.getDefaultScheduler();

        JobDetail jobDetail = JobBuilder
                .newJob(MyListenerJob.class)
                .withIdentity("job1")
                .build();

        Date endData = DateBuilder.futureDate(5, DateBuilder.IntervalUnit.SECOND);
        Trigger trigger = TriggerBuilder
                .newTrigger()
                .withIdentity("trigger1")
                .startNow()
                .withSchedule(SimpleScheduleBuilder.repeatSecondlyForever(1))
                .build();

        scheduler.scheduleJob(jobDetail,trigger);

        //调度器监听
        scheduler.getListenerManager()
                .addSchedulerListener(new MySchedulerListener());

        scheduler.start();
        Thread.sleep(5000);
        scheduler.standby();
        scheduler.shutdown();
    }
}
